import pandas as pd
import os
import datetime
import Core.Config as Config
from Utility.IndustryAnalysis import IndustrySymbolMappingSW, industry_symbol_by_name_csi
from Utility.IndustryAnalysis import get_citics_industry_mapping, get_sw_industry_mapping


# 建立行业信息
class IndustryBuilder(object):
    def __init__(self, database, industry_type="sw_industry1", update_datetime=None):
        self.database = database
        self.update_datetime = update_datetime
        self.industry_type = industry_type.lower()

        # 简单模式
        df = database.GetDataFrame("financial_data", "instrument_stock")
        self.df_stock = df

        # output
        self.industry_symbol_by_name = {}
        self.industry_name_by_symbol = {}
        self.industry_mapping = pd.DataFrame()

        #
        if self.industry_type == "csi": # 中证
            self.industry_symbol_by_name = industry_symbol_by_name_csi()

        # 中信一级
        elif self.industry_type == "citics" or self.industry_type == "citics_industry1": # 中信
            #
            df_industry_mapping = get_citics_industry_mapping()
            df_industry_mapping.drop_duplicates("citics_industry1_symbol", inplace=True)
            self.industry_mapping = df_industry_mapping[["citics_industry1_symbol", "citics_industry1_name"]].copy()

        # 中信二级
        elif self.industry_type == "citics_industry2":
            self.industry_mapping = get_citics_industry_mapping()

        elif self.industry_type == "sw_old":
            self.industry_symbol_by_name = IndustrySymbolMappingSW()

        # 申万一级
        elif self.industry_type == "sw" or self.industry_type == "sw_industry1":
            # self.industry_symbol_by_name = industry_symbol_by_name_sw_v2() # 2022屏蔽 用49行的行业列表构建填充代码
            df_industry_mapping = get_sw_industry_mapping()
            df_industry_mapping.drop_duplicates("sw_industry1_symbol", inplace=True)
            self.industry_mapping = df_industry_mapping[["sw_industry1_symbol", "sw_industry1_name"]].copy()
            a = 0

        # 申万二级
        elif self.industry_type == "sw_industry2":
            self.industry_mapping = get_sw_industry_mapping()

        else:
            pass

        # 行业列表构建填充代码
        for index, row in self.industry_mapping.iterrows():
            industry_name = row[industry_type + "_name"]
            industry_symbol = row[industry_type + "_symbol"]
            self.industry_name_by_symbol[industry_symbol] = industry_name
            self.industry_symbol_by_name[industry_name] = industry_symbol
        #
        self.industry_name_list = list(self.industry_symbol_by_name.keys())
        self.industry_symbol_list = list(self.industry_symbol_by_name.values())

    def build_industry_mapping(self, industry_symbol_by_name):
        document_list = []
        for index, value in industry_symbol_by_name.items():
            document_list.append({"industry_name": index, "industry_symbol": value})
        return pd.DataFrame(document_list)

    def get_industry_symbol_by_name(self):
        return self.industry_symbol_by_name

    def get_industry_name_list(self):
        return self.industry_name_list

    def get_industry_symbol_list(self):
        return self.industry_symbol_list

    def get_industry_stock_content_list(self, industry_name, update_datetime):
        df_industry_content = self.df_stock[["industry"] == industry_name]
        return df_industry_content

    def get_industry_symbol(self, industry_name):
        return self.industry_symbol_by_name.get(industry_name)

    def get_industry_name(self, industry_symbol):
        return self.industry_name_by_symbol.get(industry_symbol)

    def get_industry_mapping(self):
        return self.industry_mapping

    def get_industry_mapping2(self, remove_misc=True):
        df_industry_mapping = self.industry_mapping.copy()
        industry_type = self.industry_type
        df_industry_mapping.rename(columns={industry_type + "_name": "industry_name"}, inplace=True)
        df_industry_mapping.rename(columns={industry_type + "_symbol": "industry_symbol"}, inplace=True)
        #
        if remove_misc:
            df_industry_mapping = df_industry_mapping[~df_industry_mapping["industry_name"].str.contains("综合")].copy()
        return df_industry_mapping


if __name__ == '__main__':
    # ---Connecting Database---
    path_filename = os.getcwd() + r"\..\Config\config_local.json"
    database = Config.create_database(database_type="MySQL", config_file=path_filename, config_field="MySQL")

    update_datetime = datetime.datetime(2015,1,1)
    industry_builder = IndustryBuilder(database, industry_type="citics", update_datetime=update_datetime)
    print(industry_builder.industry_symbol_by_name)